
//
//  SiteDetailViewController.m
//  Rail
//
//  Created by hychen on 2018/3/6.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "SiteDetailViewController.h"
#import "LineDetailDescTableViewCell.h"
#import "TitleTableViewCell.h"
#import "WarnLevelConfig.h"
#import "FactoryDetailViewController.h"

@interface SiteDetailViewController ()
@property (nonatomic, strong) StationDetailModel *detailModel;
@property (nonatomic, strong) NSMutableArray *secTwoArray;
@end

@implementation SiteDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationItem addSimpleTitleView:@"站点详情"];
    [self.tableView.mj_header beginRefreshing];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)refreshData {
    BMWeakSelf
    [HttpRequest loadStationDetailDataWithStationId:self.stationId success:^(StationDetailModel *object) {
        weakSelf.detailModel = object;
        [weakSelf setupDataArray];
        [weakSelf.tableView reloadData];
        [weakSelf.tableView.mj_header endRefreshing];
    } failure:^(NSString *code, NSString *msg) {
        [weakSelf.tableView.mj_header endRefreshing];
        Show_Msg_Error(msg)
    }];
}

- (void)setupDataArray {
    NSArray *secOneArray = [self.detailModel area_list]?:@[].mutableCopy;
    NSArray *secTwoArray = [self setupSectionTwoArrayData];
    self.dataArray = [NSMutableArray arrayWithObjects:secOneArray,secTwoArray, nil];
}

- (NSMutableArray *)setupSectionTwoArrayData {
    NSString *opTime = [BMUtils chineseYYYYMMDDHHmmWithServiceTime:self.detailModel.op_time]?:@"";
    return @[
             @{@"title":@"站点名称",@"content":self.detailModel.station_name?:@""},
             @{@"title":@"站点状态",@"content":[WarnLevelConfig lineWorkStatusString:self.detailModel.status]},
             @{@"title":@"所属线路",@"content":self.detailModel.line_name?:@""},
             @{@"title":@"操作人员",@"content":self.detailModel.op_name?:@""},
             @{@"title":@"操作时间",@"content":opTime},
             ].mutableCopy;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *array = [self.dataArray objectAtIndex:section];
    return array.count;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *bgView = [UIView new];
    bgView.backgroundColor = ColorSepLine;
    return bgView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10.f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        NSArray *array = [self.dataArray objectAtIndex:0];
        TitleTableViewCell *cell = [TitleTableViewCell cellWithInitStyleTableView:tableView class:[TitleTableViewCell class]];
        LineWorkAreaModel *model = [array objectAtIndex:indexPath.row];
        [cell setTitle:model.area_name];
        return cell;
    }
    else if (indexPath.section == 1) {
        LineDetailDescTableViewCell *cell = [LineDetailDescTableViewCell cellWithTableView:tableView class:[LineDetailDescTableViewCell class]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSDictionary *dict = [[self.dataArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
        
        [cell.titleLabel setText:[dict objectForKeySafe:@"title"]?:@""];
        [cell.contentLabel setText:[dict objectForKeySafe:@"content"]?:@""];
        return cell;
    }
    return [UITableViewCell new];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        LineWorkAreaModel *model = self.dataArray[indexPath.section][indexPath.row];
        //跳转工区
        FactoryDetailViewController *vc = [FactoryDetailViewController new];
        vc.areaid = model.areaid;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 50;
    }
    else if (indexPath.section == 1) {
        return 40.f;
    }
    return 0;
}

@end
